/*
 * Android Runner is a multiplayer GPS game fully written by Xurxo Mendez Perez
 * 
 * Copyright (C) 2009 Xurxo Mendez Perez
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.model.persistence.message.entity;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import es.sonxurxo.androidrunner.model.persistence.game.entity.Game;
import es.sonxurxo.androidrunner.model.persistence.player.entity.Player;

/**
 * 
 * @author "Xurxo Mendez Perez"
 *
 */
@Entity
public class Message {

	public static final int SYSTEM_PLAYER_GETS_ITEM_MESSAGE = 0;
	public static final int PLAYER_PERSONAL_MESSAGE = 1;
	public static final int PLAYER_BROADCAST_MESSAGE = 2;
	public static final int SYSTEM_PLAYER_ABANDONED_MESSAGE = 3;
	private long messageId;
	private String messageBody;
	private Calendar date;
	private Player sender = null;
	private Player receiver = null;
	private boolean readed = false;
	private int type;
	private Game game;

	public Message() {}

	public Message(String messageBody, Calendar date, Player sender, 
			Player receiver, int type, Game game) {
		super();
		this.messageBody = messageBody;
		this.date = date;
		this.sender = sender;
		this.receiver = receiver;
		this.type = type;
		this.game = game;
	}

	public String getMessageBody() {
		return messageBody;
	}

	public void setMessageBody(String body) {
		this.messageBody = body;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getDate() {
		return date;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "gameId")
	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO,
            generator="MessageIdGenerator")
    @SequenceGenerator(             // It only takes effect for
            name="MessageIdGenerator", // databases providing identifier
            sequenceName="MessageSeq") // generators.
	public long getMessageId() {
		return messageId;
	}

	public void setMessageId(long messageId) {
		this.messageId = messageId;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "receiverId")
	public Player getReceiver() {
		return receiver;
	}

	public void setReceiver(Player receiver) {
		this.receiver = receiver;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "senderId")
	public Player getSender() {
		return sender;
	}

	public void setSender(Player sender) {
		this.sender = sender;
	}
	
	public String toString() {
		return new String("messageId = " + this.messageId +
				" ;body = " + this.messageBody);
	}

	public boolean isReaded() {
		return readed;
	}

	public void setReaded(boolean readed) {
		this.readed = readed;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
}
